#include "xinxiwidget.h"
#include "ui_xinxiwidget.h"
#include "tabledelegates.h"

#include <QComboBox>
#include <QDateEdit>
#include <QFormLayout>
#include <QLabel>
#include <QLineEdit>
#include <QFileDialog>
#include <QStandardPaths>
#include <QBuffer>
#include <QMessageBox>
#include <QSqlError>

xinxiWidget::xinxiWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::xinxiWidget)
{
    ui->setupUi(this);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(100);

    //性别列代理
    ComboBoxDelegale* genderDelegate = new ComboBoxDelegale(this);
    genderDelegate->setItems(QStringList()<<"男"<<"女");
    ui->tableWidget->setItemDelegateForColumn(2, genderDelegate);
    //进度列代理
    ComboBoxDelegale* progressDelegate = new ComboBoxDelegale(this);
    progressDelegate->setItems(QStringList()<<"0%" <<"20%" <<"40%" <<"60%"<< "80%" <<"100%");
    ui->tableWidget->setItemDelegateForColumn(6, progressDelegate);

    //日期代理
    ui->tableWidget->setItemDelegateForColumn(3,new DateEditDelegate(this));
    ui->tableWidget->setItemDelegateForColumn(4,new DateEditDelegate(this));

    //图片代理
    ui->tableWidget->setItemDelegateForColumn(7,new ImageDelegate(this));
    refreshTable();
}

xinxiWidget::~xinxiWidget()
{
    delete ui;
}

void xinxiWidget::refreshTable()
{
    ui->tableWidget->blockSignals(true);
    ui->tableWidget->setRowCount(0);
    QSqlQuery query("SELECT * FROM studentInfo");
    while(query.next()){
        int row=ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(row);
        for (int col = 0; col < ui->tableWidget->columnCount(); ++col) {
            QTableWidgetItem* item =new QTableWidgetItem();
            item->setTextAlignment(Qt::AlignCenter);
            if(col==ui->tableWidget->columnCount()-1){
                QByteArray photoData = query.value(col).toByteArray();
                if(!photoData.isEmpty()){
                    QPixmap photo;
                    photo.loadFromData(photoData);
                    item->setData(Qt::DecorationRole,photo.scaled(100,100,Qt::KeepAspectRatio));
                    item->setData(Qt::UserRole,photoData);
                }
            }else {
                item->setText(query.value(col).toString());
            }
            ui->tableWidget->setItem(row,col,item);
        }
    }
    ui->tableWidget->blockSignals(false);
}

QGroupBox *xinxiWidget::createFormGroup()
{
    QGroupBox *formGroup=new QGroupBox("基本信息");
    formGroup->setStyleSheet("QGroupBox {"
                              "  background-color:rgb(181, 181, 181);"    // 主背景色
                              "}");

    QFormLayout* formLayout = new QFormLayout (formGroup);
    //初始化控件

    QLineEdit* idEdit = new QLineEdit();
    idEdit->setStyleSheet("QLineEdit {"
                              "  background-color:rgb(181, 181, 181);"    // 主背景色
                              "}");
    idEdit->setObjectName("idEdit");
    QLineEdit* nameEdit = new QLineEdit();
    nameEdit->setObjectName("nameEdit");
    QComboBox* genderCombo = new QComboBox();
    genderCombo->setObjectName("genderCombo");
    QDateEdit* birthdayEdit = new QDateEdit(QDate::currentDate());
    birthdayEdit->setObjectName("birthdayEdit");
    QDateEdit* joinDateEdit = new QDateEdit (QDate::currentDate());
    joinDateEdit->setObjectName("joinDateEdit");
    QLineEdit* goalEdit = new QLineEdit();
    goalEdit->setObjectName("goalEdit");
    QComboBox* progressCombo = new QComboBox ();
    progressCombo->setObjectName("progressCombo");
    genderCombo->addItems({tr("男"), tr("女")});
    progressCombo->addItems({ tr("0%"), tr("20%"), tr("40%"), tr("60%"), tr("80%"), tr("100%")});
    birthdayEdit->setDisplayFormat("yyyy-MM-dd");
    joinDateEdit->setDisplayFormat("yyyy-MM-dd");
    birthdayEdit->setCalendarPopup(true);
    joinDateEdit->setCalendarPopup(true);
    formLayout->addRow(tr("编号: "),idEdit);
    formLayout->addRow(tr("姓名: "), nameEdit);
    formLayout->addRow(tr("性别: "), genderCombo);
    formLayout->addRow(tr("出生日期: "),birthdayEdit);
    formLayout->addRow(tr("入学日期: "),joinDateEdit);
    formLayout->addRow(tr("学习目标: "),goalEdit);
    formLayout->addRow(tr("当前进度: "),progressCombo);
    return formGroup;
}

QGroupBox *xinxiWidget::createPhotoGroup()
{
    QGroupBox *photoGroup=new QGroupBox("照片上传");
    photoGroup->setStyleSheet("QGroupBox {"
                              "  background-color:rgb(181, 181, 181);"    // 主背景色
                              "}");
    QVBoxLayout* photoLayout = new QVBoxLayout (photoGroup);
    //初始化控件
    QLabel* lblPhotoPreview = new QLabel();
    QPushButton*btnSelectPhoto=new QPushButton(tr("选择照片"));
    //配置控件
    lblPhotoPreview->setAlignment (Qt::AlignCenter);
    lblPhotoPreview->setMinimumSize(200, 200);
    btnSelectPhoto->setFixedSize(100, 40);
    //添加控件到布局
    photoLayout->addWidget (lblPhotoPreview);
    photoLayout->addWidget (btnSelectPhoto, 0, Qt::AlignHCenter);

    connect (btnSelectPhoto, &QPushButton::clicked, [this, lblPhotoPreview]() {
        QString fileName = QFileDialog::getOpenFileName(
            this,tr("选择学生照片"),
            QStandardPaths::writableLocation(QStandardPaths::PicturesLocation),
            tr("图片文件(*.png *.jpg *.jpeg)"));
        if (!fileName.isEmpty()) {
            QPixmap pixmap(fileName);
            if (!pixmap.isNull()) {
                pixmap=pixmap.scaled(//智能缩放
                    lblPhotoPreview->width() - 30,
                    lblPhotoPreview->height() - 30,
                    Qt::KeepAspectRatio
                    );
                lblPhotoPreview->setPixmap (pixmap);
                QBuffer buffer(&photoData);//转换为字节数组
                buffer.open(QIODevice::WriteOnly);
                pixmap.save (&buffer,"PNG");
            }
            else QMessageBox::warning(this,tr("错误"),tr("无法加载图片文件！"));
        }
    });
    return photoGroup;
}

void xinxiWidget::handleDialogAccepted(QGroupBox *formGroup, QGroupBox *photoGroup)
{
    //获取表单数据
    QLineEdit*idEdit = formGroup->findChild<QLineEdit*>("idEdit");
    QLineEdit*nameEdit = formGroup->findChild<QLineEdit*>("nameEdit");
    QComboBox*genderCombo = formGroup->findChild<QComboBox*>("genderCombo");
    QDateEdit*birthdayEdit = formGroup->findChild<QDateEdit*>("birthdayEdit");
    QDateEdit*joinDateEdit = formGroup->findChild<QDateEdit*>("joinDateEdit");
    QLineEdit* goalEdit= formGroup->findChild<QLineEdit*>("goalEdit");
    QComboBox* progressCombo= formGroup->findChild<QComboBox*>("progressCombo");
    //数据校验
    if (idEdit->text().isEmpty() || nameEdit->text().isEmpty()) {
        QMessageBox::warning(this,tr("错误"),tr("学号和姓名不能为空！"));
        return;
    }
    //检查学号唯一性
    QSqlQuery checkQuery;
    checkQuery.prepare("SELECT id FROM studentInfo WHERE id = ?");
    checkQuery. addBindValue(idEdit->text());
    if (checkQuery.exec() && checkQuery.next()) {
        QMessageBox::warning(this,tr("错误"),tr("学号%1 已存在！").arg(idEdit->text()));
        return;
    }

    //插入数据
    QSqlDatabase::database().transaction();
    QSqlQuery insertQuery;
    insertQuery. prepare(
        "INSERT INTO studentInfo"
        "(id, name, gender, birthday, join_date, study_goal, progress, photo)"
        "VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
        );
    //绑定参数
    insertQuery.addBindValue(idEdit->text());
    insertQuery.addBindValue (nameEdit->text());
    insertQuery.addBindValue(genderCombo->currentText());
    insertQuery.addBindValue(birthdayEdit->date().toString("yyyy-MM-dd"));
    insertQuery.addBindValue(joinDateEdit->date().toString("yyyy-MM-dd"));
    insertQuery.addBindValue(goalEdit->text());
    insertQuery.addBindValue (progressCombo->currentText());
    insertQuery.addBindValue(photoData.isEmpty() ? QVariant():photoData);
    if (!insertQuery.exec()){
        QSqlDatabase::database().rollback();
        QMessageBox::critical(this,tr("错误"),tr("添加失败:")+ insertQuery.lastError().text());
    }
    else {
        QSqlDatabase::database().commit();
        refreshTable();
        QMessageBox::information(this,tr("成功"),tr("已成功添加学生：%1").arg(nameEdit->text()));
    }


}

void xinxiWidget::on_pushButton_TianJia_clicked()
{
    QDialog dlg(this);
    dlg.setWindowTitle(tr("添加学生信息"));
    dlg.setMinimumSize(600,400);
    dlg.setStyleSheet("QDialog {"
                      "  background-color:rgb(181, 181, 181);"    // 主背景色
                      "}");

    QVBoxLayout* mainLayout =new QVBoxLayout(&dlg);
    QHBoxLayout* contentLayout = new QHBoxLayout();
    mainLayout->addLayout(contentLayout);
    QGroupBox* formGroup = createFormGroup();
    QGroupBox* photoGroup = createPhotoGroup();
    contentLayout->addWidget(formGroup,1);
    contentLayout->addWidget(photoGroup,1);

    QHBoxLayout* btnLayout = new QHBoxLayout();
    QPushButton* btnConfirm= new QPushButton(tr("确认"));
    QPushButton* btnCancel = new QPushButton(tr("取消"));
    btnConfirm->setFixedWidth(150);
    btnCancel->setFixedWidth(150);

    btnLayout->addStretch();
    btnLayout->addWidget(btnConfirm);
    btnLayout->addWidget(btnCancel);
    btnLayout->addStretch();

    connect(btnConfirm, &QPushButton::clicked, &dlg, &QDialog::accept);
    connect(btnCancel, &QPushButton::clicked, &dlg, &QDialog::reject);
    mainLayout->addLayout(btnLayout);
    if(dlg.exec() == QDialog::Accepted){
        handleDialogAccepted(formGroup,photoGroup);
    }
}


void xinxiWidget::on_pushButton_ShanChuXiang_clicked()
{
    auto selected = ui->tableWidget->selectedItems();
    if (selected.isEmpty()) {
        QMessageBox::warning(this,"警告","请先选择要删除的单元格！");
        return;
    }
    QSqlDatabase::database().transaction();
    foreach(QTableWidgetItem * item, selected) {
        int row = item->row();
        int col = item->column();
        QString id = ui->tableWidget->item(row,0)->text();
        const QStringList columns = {"id","name", "gender", "birthday",
                                     "join_date", "study_goal", "progress", "photo" };
        QSqlQuery query;
        query.prepare(QString("UPDATE studentInfo SET %1 = ? WHERE id = ?").arg(columns[col]));
        query.addBindValue("");
        query.addBindValue(id);
        if (!query.exec()){
            QSqlDatabase::database().rollback();
            QMessageBox::critical(this,"错误","更新失败:"+query.lastError().text());
            return;
        }
    }
    QSqlDatabase::database().commit();
    refreshTable();
}


void xinxiWidget::on_pushButton_ShanChuHang_clicked()
{
    auto selected = ui->tableWidget->selectionModel()->selectedRows();
    if (selected.isEmpty()) {
        QMessageBox::warning(this,"警告","请先选择要删除的行！");
        return;
    }
    QSqlDatabase::database().transaction();
    foreach(const QModelIndex & index, selected) {
        QString id = ui->tableWidget->item(index.row(),0)->text();
        QSqlQuery query;
        query.prepare("DELETE FROM studentInfo WHERE id = ?");
        query.addBindValue(id);
        if (!query.exec()) {
            QSqlDatabase::database().rollback();
            QMessageBox::critical(this,"错误","删除失败:"+query.lastError().text());
            return;
        }
    }
    QSqlDatabase::database().commit();
    refreshTable();
}

